package pc.nick.servlet;

import pc.nick.http.HttpServlet;
import pc.nick.http.HttpServletRequest;
import pc.nick.http.HttpServletResponse;
import pc.nick.http.HttpSession;

import java.io.PrintWriter;

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) {
        doPost(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        PrintWriter out = response.getWriter();
        String jsessionid = request.getJsessionid();
        if(jsessionid != null){
            out.print("HTTP/1.1 200 OK\r\nSet-Cookie: JSESSIONID=" + jsessionid + "\r\n\r\n");
        }else{
            out.print("HTTP/1.1 200 OK\r\n\r\n");
        }
        out.print("<html>");
        out.print("<body>");
        out.print("welcome! " + username + "<br/>");
        out.print("sessionId: " + jsessionid + "<br/>");
        out.print("</body>");
        out.print("</html>");
        out.flush();
        out.close();

    }
}
